package com.aier.admin.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;



import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.admin.entity.KouWei;

public class KouWeiRowMapper implements RowMapper<KouWei>{

    private int type;

    public static final int KOUWEI_AND_NUM = 1;
    
    public KouWeiRowMapper() {
    }

    public KouWeiRowMapper(int type) {
        this.type = type;
    }

    @Override
    public KouWei mapRow(ResultSet rs, int rowNum) throws SQLException {
        KouWei kw = new KouWei();
        switch (type) {
            case KOUWEI_AND_NUM:
                kw = parseKouWei(new KouWei(), rs);
                kw.setKw_num(rs.getInt("kw_num"));
                break;
            default:
                kw = parseKouWei(new KouWei(), rs);
        }
        return kw;
    }

    public static KouWei parseKouWei(KouWei kw, ResultSet rs)
        throws SQLException {
        return parseKouWei(kw, rs, null);
    }

    public static KouWei parseKouWei(KouWei kw, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }

        try {
            kw.setKw_id(rs.getLong(su + "kw_id"));
        } catch (SQLException e) {
            kw.setKw_id(null);
        }
        try {
            kw.setKw_name(rs.getString(su + "kw_name"));
        } catch (SQLException e) {
            kw.setKw_name(null);
        }
      
        return kw;
    }

    public static Map<String, Object> getPar(Map<String, Object> par,
            KouWei kw) {
        par.put("kw_id", kw.getKw_id());
        par.put("kw_name", kw.getKw_name());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args,
            KouWei kw) {
        args.put("kw_id", kw.getKw_id());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final KouWei kw) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(kw),
                        new String[] { "kw_id" });
                parsePS(ps, kw);
                return ps;
            }

        };
    }

    private static String parseSQL(KouWei kw) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into userapp_kouwei(");
        int count = 0;
        if (null != kw.getKw_id()) {
            sql.append("kw_id,");
            count++;
        }
        if (null != kw.getKw_name()) {
            sql.append("kw_name,");
            count++;
        }
        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, KouWei kw)
        throws SQLException {
        int count = 1;
        if (null != kw.getKw_id()) {
            ps.setLong(count++, kw.getKw_id());
        }
        if (null != kw.getKw_name()) {
            ps.setString(count++, kw.getKw_name());
        } 

    }

}
